using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace CommonMvc.Models.CommonModels
{
    public class Person: BasicEntityModel
    {

        public Person() {
            PersonRoles = new HashSet<PersonRole>();
        }
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id{get;set;}
        //用户名
        [Required]
        [MaxLength(50,ErrorMessage ="name 不能长于50"),MinLength(2,ErrorMessage ="name长度要大于2")]
        public String Name{get;set;}
        //用户状态
        public int PersonState{get;set;}

        public virtual ICollection<PersonRole> PersonRoles { get; set; }

        public new static void OnModelCreating(ModelBuilder modelBuilder) {
            modelBuilder.Entity<Person>()
                .Property(p => p.Name)
                .IsRequired();
            modelBuilder.Entity<Person>()
                .Property(p => p.Name)
                .HasMaxLength(50);
            modelBuilder.Entity<Person>()
                .HasMany(p => p.PersonRoles)
                .WithOne(pr => pr.Person)
                .HasForeignKey(pr => pr.PersonId);

        }
    }

     public class PersonRole : BasicEntityModel
    {
     
        //用户名
        public int  PersonId{get;set;}
        //用户状态
        public int RoleId{get;set;}

        public virtual Role Role { get; set; }

        public virtual Person Person { get; set; }
        
        public new static void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<PersonRole>()
                .HasKey(pr => new { pr.RoleId, pr.PersonId });
            modelBuilder.Entity<PersonRole>()
                .HasOne(pr => pr.Person)
                .WithMany(u => u.PersonRoles)
                .HasForeignKey(r => r.PersonId);
            modelBuilder.Entity<PersonRole>()
                .HasOne(pr => pr.Role)
                .WithMany(u => u.PersonRoles)
                .HasForeignKey(r => r.RoleId);



            // 不需要级联删除
        }

    }

     public class Role : BasicEntityModel
    {

        public Role()
        {
            PersonRoles = new HashSet<PersonRole>();
        }
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id{get;set;}
        //角色名
        public String Name{get;set;}
        //角色状态
        public int RoleState{get;set;}
        //角色备注
        public String RoleMemo{get;set;}


        public virtual ICollection<RolePermission> RolePermissions { get; set; }

        public virtual ICollection<PersonRole> PersonRoles { get; set; }

        public new static void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Role>()
                .HasMany(r => r.PersonRoles)
                .WithOne(pr => pr.Role)
                .HasForeignKey(pr => pr.RoleId);
        }
    }

        public class RolePermission : BasicEntityModel
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id{get;set;}
        //角色Id
        public int RoleId{get;set;}
        //控制器权限名称
        public String Controller{get;set;}
        //方法权限名称
        public String Action{get;set;}

        public virtual Role Role { get; set; }

        public new static void OnModelCreating(ModelBuilder modelBuilder)
        {

        }
    }




}